package com.dst.utli;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

public class JwtUtils {
  private static final String SING="HELLOWORLD";
  /**
   * 生成token
   */
  public static String getToken(Map<String,String> map){
    JWTCreator.Builder builder= JWT.create();
    map.forEach((k,v)->{
      builder.withClaim(k,v);
    });
    Calendar instance = Calendar.getInstance();
    //设置token七天过期
    instance.add(Calendar.DATE,7);

    String token = builder.withExpiresAt(instance.getTime())//指定过期时间
      .sign(Algorithm.HMAC256(SING));
    return token;
  }

  /**
   * 获取token里面的信息，如果验证失败会抛出异常
   * @param token
   * @return
   */
  public static DecodedJWT verify(String token){
    return JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
  }
}
